package toy.keli.edic.view

import androidx.collection.arrayMapOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.TextUnit
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import toy.keli.edic.data.Db
import toy.keli.edic.data.model.Article
import toy.keli.edic.data.model.Query
import toy.keli.edic.ui.main.extend.bgRun
import toy.keli.edic.util.DateUtil
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit


open class RichTextFont(
        val fontSize: TextUnit = TextUnit.Unspecified,
        val color: Color = Color.Unspecified,
        val bgColor: Color = Color.Unspecified,
        val fontFamily: FontFamily? = null,
        val fontStyle: FontStyle? = null,
        val fontWeight: FontWeight? = null,
        var words: List<String> =listOf(),
        val type:RichTextFonts.Type? = null,
        val level: Int = 0,
    ) {
    open fun find(part: String): Boolean {
        if(words.size ==0 && type==null) return true
        return words.indexOf(part) > -1
    }

    companion object{
        var topAll = setOf<String>()
        var hAll = setOf<String>()
        data class BeforLevle(var top:Int = 3,var history:Int = 0)
        val beforLevle = BeforLevle()
        fun initTopAll(level: Int) {
            if(topAll.size>0 &&  beforLevle.top == level) return
            topAll = Article.getTop(level,true).toSet()
            beforLevle.top = level
        }
        //获取最近的数据历史记录数据
        fun initHistoryWords(words: List<String> = listOf(),day:Int=100) {
            if(hAll.size>0 &&  beforLevle.history == day) return;
            val hQuery = Query(Db.user, "historyTable")
//                .select("word")
                .order("time desc")
                .limit(5000)
            if(words.size>0) hQuery.whereIn("word",words)
            //天数为不为最大值就限制天数 天数为0就不要配置历史数据
            if(day<100){
                hQuery.where("time","<", DateUtil.getDaysAgoTimestamp(day.toLong())*1000)
            }else if(day<1){
                return
            }
            val countDownLatch = CountDownLatch(1)
            bgRun  {
                hAll = Db.user.wordModelDao.getHistoryList(hQuery.build()).map {
                    it.word
                }.toSet()
                countDownLatch.countDown()
            }
            countDownLatch.await(500, TimeUnit.MILLISECONDS)
            beforLevle.history = day
        }
    }
    fun initWords(words: List<String>) {
        when(type){
            RichTextFonts.Type.top -> {
                initTopAll(level)
                this.words = topAll.intersect(words).toList()
            }
            RichTextFonts.Type.history ->{
                initHistoryWords(day=level)
                this.words = hAll.intersect(words).toList()
            }
            else ->{

            }
        }
    }
}

class RichTextFonts(vararg val fonts: RichTextFont) {
    enum class Type {
        top,     //top0-7 8个等级共计4000+个单词
        history, //历史数据
        level    //级别数据
    }
    fun setWords(words:List<String>){
        fonts.forEach {
            it.initWords(words)
        }
    }
    fun selectByText(part: String): RichTextFont {
        fonts.forEach {
            if(it.find(part.lowercase())) return it
        }
        return RichTextFont()
    }
}